package dao.impl;

import dao.BaseDao;
import dao.GoodDao;
import domain.Goods;
import domain.User;
import utils.JDBCUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author HLF
 * @version V1.0
 * @package dao.impl
 * @date 2021/10/31 12:18
 */
public class GoodDaoImpl extends BaseDao implements GoodDao {
    @Override
    public int addGoods(Goods goods) {
        Connection conn = null;
        int insert = 0;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "insert into goods(name,price,num,img) values(?,?,?,?);";
            insert = update(conn, sql, goods.getName(), goods.getPrice(), goods.getNum(), goods.getImg());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return insert;
    }

    @Override
    public int deleteGoodsById(Integer id) {
        Connection conn = null;
        int delete = 0;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "delete from goods where id = ?;";
            delete = update(conn, sql, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return delete;
    }

    @Override
    public int updateGoods(Goods goods) {
        Connection conn = null;
        int update = 0;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "update goods set name = ?,price = ?,num = ? ,img = ? where id = ?;";
            update = update(conn, sql, goods.getName(), goods.getPrice(), goods.getNum(), goods.getImg(), goods.getId());
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return update;
    }

    @Override
    public Goods queryGoodsById(Integer id) {
        Connection conn = null;
        Goods goods = null;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "select id,name ,price,num,img from goods where id = ? ;";
            goods = queryForOne(conn, sql, Goods.class, id);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return goods;
    }

    @Override
    public List<Goods> queryGoods() {
        Connection conn = null;
        List<Goods> list = null;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "select id,name ,price,num,img from goods;";
            list = queryForMore(conn, sql, Goods.class);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return list;
    }

    @Override
    public List<Goods> queryGoodsByName(String name) {
        Connection conn = null;
        List<Goods> list = null;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "select id,name ,price,num,img from goods where name like ?;";
            String likeName = "%"+name+"%";//like模糊查询
            list = queryForMore(conn, sql, Goods.class, likeName);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return list;
    }

    @Override
    public List<Goods> queryGoodsByPrice(BigDecimal minPrice, BigDecimal maxPrice) {
        Connection conn = null;
        List<Goods> list = null;
        try {
            conn = JDBCUtils.getDruidConnection();
            String sql = "select id,name ,price,num,img from goods where price between ? and ?;";
            list = queryForMore(conn, sql, Goods.class, minPrice,maxPrice);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            JDBCUtils.closeResource(conn, null);
        }
        return list;
    }
}
